package com.item.model.config;

import com.item.model.filter.HttpServletFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author zhang-rongyao
 * @version V1.0
 * @Package com.item.model.config
 * @date 2021/7/26 10:36
 */
@ServletComponentScan("com.item.model.filter")
@Configuration
public class FilterConfig {

    @Autowired
    private HttpServletFilter httpServletFilter;

    /**
     * 解决requestBody只能读取一次
     * @return
     */
    @Bean
    public FilterRegistrationBean registerAuthFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(httpServletFilter);
        registration.addUrlPatterns("/order/*");
        registration.setName("orderFilter");
        //值越小，Filter越靠前。 但是有时候这个setOrder(1)不一定生效，所以一定要setName
        registration.setOrder(1);
        return registration;
    }

    //如果有多个Filter，再写一个public FilterRegistrationBean registerOtherFilter(){...}即可。
}
